package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @program: myitcast_health
 * @ClassName: AddressController
 * @description:
 * @author: xiechangyu
 * @create: 2020-08-28 23:15
 **/
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    @RequestMapping("/findAllAddress")
    public Result findAllAddress(){
        List<String> list = addressService.findAllAddress();
        List<Map<String,Object>> mapList=new ArrayList<>();
        Map<String,Object> map1=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        map1.put("id",-9);
        map1.put("address","");
        map1.put("label","a");
        map2.put("id",-9);
        map2.put("address","");
        map2.put("label","b");
        mapList.add(map1);
        mapList.add(map2);


        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map=new HashMap<>();
            String s = list.get(i);
            map.put("id",i);
            map.put("val",s);
            map.put("lable",""+i);
            mapList.add(map);
        }

        Map<String,Object> map3=new HashMap<>();
        Map<String,Object> map4=new HashMap<>();
        map3.put("id",-9);
        map3.put("address","");
        map3.put("label","c");
        map4.put("id",-9);
        map4.put("address","");
        map4.put("label","d");
        mapList.add(map3);
        mapList.add(map4);
        return new Result(true,"查询数据成功",mapList);
    }
    @RequestMapping("/findAllAddressAndId")
    public Result findAllAddressAndId(){
        //查出所有地址信息,封装成bean
        List<Address> list=addressService.findAllAddress2();
        if(list!=null&&list.size()>0){
            List<Integer> listIds=new ArrayList<>();
            List<String> listAddressNames=new ArrayList<>();
            //将地址id和地址名字封装成map集合
            Map<Integer,String> map=new HashMap<>();
            for (Address address : list) {
                int listId=address.getId();
                listIds.add(listId);
                String listAddressName=address.getAddressname();
                listAddressNames.add(listAddressName);
                map.put(listId,listAddressName);
            }
            return new Result(true,"查询地址成功",listAddressNames);
        }
        return new Result(false,"查询地址失败");

    }

}
